//
//  SuperviseTaskBasicView.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/1/9.
//

import Foundation
import UIKit
import Kingfisher

class SuperviseTaskBasicView: UIScrollView {
    let taskItem = InspListDetailItem(withKeyWidth: 110) // 检查任务
    let liableItem = InspListDetailItem(withKeyWidth: 110) // 负责人
    let checkDateItem = InspListDetailItem(withKeyWidth: 110) // 检查时间
    let submitDateItem = InspListDetailItem(withKeyWidth: 110) // 提交时间
    let arrageItem = InspListDetailItem(withKeyWidth: 110) // 整改负责人
    let addrItem = InspListDetailItem(withKeyWidth: 110) // 检查地点
    
    let signItem = STaskSignView(withTitle: "检查人签字") // 检查人签字
    let beSignItem = STaskSignView(withTitle: "被检查人签字") // 被检查人签字

    override init(frame: CGRect) {
        super.init(frame: frame)
        showsVerticalScrollIndicator = false
        setupUI()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
        
    func reload(withModal modal: SpRwbgxqModal?) {
        taskItem.update(withKey: "检查任务:", value: modal?.rwlxmc)
        liableItem.update(withKey: "负责人:", value: modal?.fzrmc)
        checkDateItem.update(withKey: "检查时间:", value: "\(modal?.rwkssj ?? "--")~\(modal?.rwjssj ?? "--")")
        submitDateItem.update(withKey: "提交时间:", value: modal?.tjsj)
        arrageItem.update(withKey: "整改负责人:", value: modal?.zgfzrmc)
        addrItem.update(withKey: "检查地点:", value: modal?.wdmc)
        
        if let xjrqms = modal?.xjrqms {
            for (index, item) in xjrqms.enumerated() {
                if item.fjdz != nil {
                    let url = BaseURL + (item.fjdz)!
                    if index == 0 {
                        signItem.sign1IV.kf.setImage(with: URL(string: url))
                    }
                    if index == 1 {
                        signItem.sign2IV.kf.setImage(with: URL(string: url))
                    }
                }
            }
        }
        
        if modal?.bjrqz != nil {
            let url = BaseURL + (modal?.bjrqz)!
            beSignItem.sign1IV.kf.setImage(with: URL(string: url))
        }
    }
    
    func setupUI() {
        let basicV = UIView.createBase()
        addSubview(basicV)
        basicV.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(10)
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.width.equalTo(ScreenWidth - 20)
        }
        
        basicV.addSubview(taskItem)
        taskItem.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(50)
        }

        basicV.addSubview(liableItem)
        liableItem.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(taskItem.snp.bottom)
            make.height.equalTo(taskItem.snp.height)
        }
        
        basicV.addSubview(checkDateItem)
        checkDateItem.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(liableItem.snp.bottom)
            make.height.equalTo(taskItem.snp.height)
        }
        
        basicV.addSubview(submitDateItem)
        submitDateItem.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(checkDateItem.snp.bottom)
            make.height.equalTo(taskItem.snp.height)
        }
        
        basicV.addSubview(arrageItem)
        arrageItem.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(submitDateItem.snp.bottom)
            make.height.equalTo(taskItem.snp.height)
        }
        
        basicV.addSubview(addrItem)
        addrItem.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(arrageItem.snp.bottom)
            make.height.equalTo(taskItem.snp.height)
            make.bottom.equalToSuperview()
        }
        
        addSubview(signItem)
        signItem.snp.makeConstraints { make in
            make.top.equalTo(basicV.snp.bottom).offset(10)
            make.height.equalTo(100)
            make.centerX.equalToSuperview()
            make.width.equalTo(ScreenWidth - 20)
        }
        
        addSubview(beSignItem)
        beSignItem.snp.makeConstraints { make in
            make.top.equalTo(signItem.snp.bottom).offset(10)
            make.bottom.equalToSuperview().offset(-10)
            make.height.equalTo(100)
            make.centerX.equalToSuperview()
            make.width.equalTo(ScreenWidth - 20)
        }
    }
}

// 签字
class STaskSignView: UIView {
    var title: String? {
        didSet {
            titleL.text = title
        }
    }
    
    let titleL = UILabel()
    
    let sign1IV = UIImageView()
    let sign2IV = UIImageView()

    var imgs: [UIImage] = []
    
    init(withTitle title: String) {
        super.init(frame: .zero)
        setupUI()
        titleL.text = title
        layer.cornerRadius = 10
        layer.masksToBounds = true
        backgroundColor = .white
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        titleL.textColor = .hex("#666666")
        titleL.font = UIFont.systemFont(ofSize: 14)
        addSubview(titleL)
        titleL.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(self.snp.left).offset(11)
            make.width.equalTo(90)
        }
        
        let signWidth = (ScreenWidth - 200) / 2.0
        addSubview(sign1IV)
        sign1IV.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(titleL.snp.right).offset(20)
            make.bottom.equalToSuperview().offset(-20)
            make.width.equalTo(signWidth)
            make.height.equalTo(50)
        }
        
        addSubview(sign2IV)
        sign2IV.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(sign1IV.snp.right).offset(20)
            make.bottom.equalToSuperview().offset(-20)
            make.width.equalTo(signWidth)
            make.height.equalTo(50)
        }
    }
}
